package com.ids.util;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ids.bean.ForgotPassword;

public class Utlity {
	
//	private static final String username = "aaaa@gmail.com";
//	private static final String password = "aaaa";
//	private static final String subject = "Forgot Password";
//	 private static final String link = "http://localhost:8080/abc/RecoverPassword.action?key=";
	
	public final static String[] monthcalender = { "January", "February",
			"March", "April", "May", "June", "July", "August", "September",
			"October", "November", "December" };

	public static String MD5(String text) throws NoSuchAlgorithmException,UnsupportedEncodingException {
		MessageDigest md;
		md = MessageDigest.getInstance("MD5");
		byte[] md5hash = new byte[32];
		md.update(text.getBytes("iso-8859-1"), 0, text.length());
		md5hash = md.digest();
		return convertToHex(md5hash);
	}

	private static String convertToHex(byte[] data) {
		StringBuffer buf = new StringBuffer();
		int halfbyte;
		int two_halfs;
		for (int i = 0; i < data.length; i++) {
			halfbyte = (data[i] >>> 4) & 0x0F;
			two_halfs = 0;
			do {
				if ((0 <= halfbyte) && (halfbyte <= 9))
					buf.append((char) ('0' + halfbyte));
				else
					buf.append((char) ('a' + (halfbyte - 10)));
				halfbyte = data[i] & 0x0F;
			} while (two_halfs++ < 1);
		}
		return buf.toString();
	}

	public static String link1 = " ";
	public void getInfo(String contextPath, int port, String hostName) {
		link1 = "http:" + "//" + hostName + ":" + port + contextPath
				+ "/RecoverPassword.action?key=";
	}

	public static void sendMail(ForgotPassword fp) {
		// Properties props = new Properties();
		// props.put("mail.smtp.host", "smtp.gmail.com");
		// props.put("mail.smtp.socketFactory.port", "465");
		// props.put("mail.smtp.socketFactory.class",
		// "javax.net.ssl.SSLSocketFactory");
		// props.put("mail.smtp.auth", "true");
		// props.put("mail.smtp.port", "465");
		//
		// Session session = Session.getDefaultInstance(props,
		// new javax.mail.Authenticator() {
		// protected PasswordAuthentication getPasswordAuthentication() {
		// return new PasswordAuthentication(username, password);
		// }
		// });
		//
		// try {
		//
		// Message message = new MimeMessage(session);
		// message.setFrom(new InternetAddress(username));
		// message.setRecipients(Message.RecipientType.TO, InternetAddress
		// .parse(fp.getEmail()));
		// message.setSubject(subject);
		// message
		// .setText(link1 + fp.getKey() + "&empCode="
		// + fp.getEmpCode());
		//
		// Transport.send(message);
		//
		// } catch (MessagingException e) {
		// throw new RuntimeException(e);
		// }
	}

	public static void sendMailAccountConfirmation(String email,
			String empCode, String pass) {
		// Properties props = new Properties();
		// props.put("mail.smtp.host", "smtp.gmail.com");
		// props.put("mail.smtp.socketFactory.port", "465");
		// props.put("mail.smtp.socketFactory.class",
		// "javax.net.ssl.SSLSocketFactory");
		// props.put("mail.smtp.auth", "true");
		// props.put("mail.smtp.port", "465");
		//
		// Session session = Session.getDefaultInstance(props,
		// new javax.mail.Authenticator() {
		// protected PasswordAuthentication getPasswordAuthentication() {
		// return new PasswordAuthentication(username, password);
		// }
		// });
		//
		// try {
		//
		// Message message = new MimeMessage(session);
		// message.setFrom(new InternetAddress(username));
		// message.setRecipients(Message.RecipientType.TO, InternetAddress
		// .parse(email));
		// message.setSubject("Account Creation");
		// message.setText("EmpCode: " + empCode + "Password: " + pass);
		//
		// Transport.send(message);
		//
		// } catch (MessagingException e) {
		// throw new RuntimeException(e);
		// }
	}

	public static int[] displayMonthDays(int month ,int year) {
		Calendar c = new GregorianCalendar();
		c.set(year, month-1, 1);
		int[] displayDays = new int[c.getActualMaximum(Calendar.DATE)];
		for (int i = 0; i < displayDays.length; i++) {
			displayDays[i] = i + 1;
		}
		return displayDays;
	}
//	public static int[] displayMonth() {
//		Calendar c = new GregorianCalendar();
//		int[] displayDays = new int[c.getActualMaximum(Calendar.DATE)];
//		for (int i = 0; i < displayDays.length; i++) {
//			displayDays[i] = i + 1;
//		}
//		return displayDays;
//	}

	public static List<String> displayWeekDays(int month ,int year) {
		List<String> displayWeekDays = new ArrayList<String>();
		final String[] weekDays = { "S", "M", "T", "W", "T", "F", "S" };
		Calendar c = new GregorianCalendar();
		c.set(year, month-1, 1);
		for (int i = 1; i <= c.getActualMaximum(Calendar.DATE); i++) {
			c.set(Calendar.DATE, i);
			displayWeekDays.add(weekDays[c.get(Calendar.DAY_OF_WEEK) - 1]);
		}
		return displayWeekDays;
	}
	public static List<String > displayAttendanceStatistics(){
		final String[] attendanceStatistics = { "P", "SL", "CL", "PL", "CO", "LW", "WO","SH","%" };
		List<String> displayAttendanceStatistics = Arrays.asList(attendanceStatistics);
		return displayAttendanceStatistics;
	}

	public static String getMonthMM() {
		Date d = new Date();
		Calendar todaycldr = Calendar.getInstance();
		return monthcalender[d.getMonth()] + " " + todaycldr.get(Calendar.YEAR);
	}
	public static Map<Integer,String> getNoOfDays(){
		Map<Integer,String>  getNoOfDaysMap = new TreeMap<Integer,String>();
		getNoOfDaysMap.put(3, "3"); 
		getNoOfDaysMap.put(4, "4"); 
		getNoOfDaysMap.put(5, "5"); 
		getNoOfDaysMap.put(6, "6"); 
		getNoOfDaysMap.put(7, "7"); 
		getNoOfDaysMap.put(8, "8"); 
		getNoOfDaysMap.put(9, "9"); 
		getNoOfDaysMap.put(10, "10"); 
		getNoOfDaysMap.put(11, "11"); 
		getNoOfDaysMap.put(12, "12"); 
		getNoOfDaysMap.put(13, "13"); 
		getNoOfDaysMap.put(14, "14"); 
		getNoOfDaysMap.put(15, "15"); 
		 
		 
		
				return getNoOfDaysMap;
	}
//	public static void downloadFile(String original_filename, HttpServletRequest request,
//            HttpServletResponse response) throws ServletException, IOException {
//
//		  String filename = "TestFile.xml";
//	        File f = new File(original_filename);
//	        int length = 0;
//	        ServletOutputStream op = response.getOutputStream();
//	       
//	        ServletContext context = request.getSession().getServletContext();
//	        String mimetype = context.getMimeType(original_filename);
//
//	        // Set the response and go!
//	        response.setContentType((mimetype != null) ? mimetype
//	                : "application/octet-stream");
//	        response.setContentLength((int) f.length());
//	        response.setHeader("Content-Disposition", "attachment; filename=\""
//	                + original_filename + "\"");
//
//	        // Stream to the requester.
//	        byte[] bbuf = new byte[original_filename.length()];
//	        DataInputStream in = new DataInputStream(new FileInputStream(f));
//
//	        while ((in != null) && ((length = in.read(bbuf)) != -1)) {
//	            op.write(bbuf, 0, length);
//	        }
//	        in.close();
//	        op.flush();
//	        op.close();
//    }
	public static File getFile(String data){
		OutputStream out = null;
		String fileName = "abc.csv";
		File file = new File(fileName);
		try {
		file.createNewFile();
		out = new FileOutputStream(file);
		} catch (IOException e1) {
		e1.printStackTrace();
		}

//		String s = "Hello";
		try {
		out.write(data.getBytes());
		} catch (IOException e) {
		e.printStackTrace();
		}

		return file;
	}

}